/**
 * Created by fifths on 17-8-31.
 */

import {Injectable} from '@angular/core';
import {HttpService} from '../../../../lib/service/http.service';
import {adSystemApiUrl} from '../../../../../config/api';
import {Http, Headers, RequestOptions, RequestOptionsArgs} from '@angular/http';


@Injectable()
export class ShowCaseService {

  constructor(private httpService: HttpService) {
  }

  // 获取展位列表
  getShowcaseList(template_id: number, page = 1, pageSize = 999) {
    const url = `${adSystemApiUrl}/v1/member/show_case/list` +
      `?template_id=${template_id}&order={"sort":"asc"}&page=${page}&pageSize=${pageSize}`;
    return this.httpService.httpGetAuth(url).map(rs => rs);
  }

  // 获取展位单元列表
  getShowCaseDetail(applet_uuid: number, showcase_id: number, page = 1, pageSize = 999) {
    const url = `${adSystemApiUrl}/v1/member/show_case_detail/list` +
      `?applet_uuid=${applet_uuid}&showcase_id=${showcase_id}&order={"sort":"asc"}&page=${page}&pageSize=${pageSize}`;
    return this.httpService.httpGetAuth(url).map(rs => rs);
  }

  // 获取展位单元详情
  getUnitDetail(id: number) {
    const url = `${adSystemApiUrl}/v1/member/show_case_detail/${id}/info`;
    return this.httpService.httpGetAuth(url).map(rs => rs);
  }

  // 删除展位单元
  delShowCaseDetail(id: number) {
    const url = `${adSystemApiUrl}/v1/member/show_case_detail/${id}/del`;
    return this.httpService.httpPostAuth(url, {}).map(rs => rs);
  }

  // 商品列表
  getGoodsList(applet_uuid: string,
               page: number,
               pageSize: number,
               applet_goods_category_id?: number,
               status?: string, order?: string,
               keyword?: string) {
    const _applet_goods_category_id = applet_goods_category_id || '',
      _status = (status === '选择商品状态' ? '' : status) || '',
      _keyword = keyword || '';
    const _order = (order === 'none' ? '' : order) || '';
    const url = `${adSystemApiUrl}/v1/member/goods/list` +
      `?applet_uuid=${applet_uuid}&applet_goods_category_id=${_applet_goods_category_id}` +
      `&order=${_order}&status=${_status}&keyword=${_keyword}&page=${page}&pageSize=${pageSize}`;
    return this.httpService.httpGetAuth(url).map(rs => rs);
  }

  // 获取卡券列表
  getCouponList(page: number,
                pageSize: number,
                keyword: string) {
    const url = `${adSystemApiUrl}/v2/member/card_coupons/list`;
    const headers = new Headers({'Content-Type': 'application/json'});
    const options = new RequestOptions({headers: headers});
    const body = JSON.stringify({
      page: page,
      pageSize: pageSize,
      keyword: keyword
    });
    return this.httpService.httpPostAuth(url, body, options).map(rs => rs);
  }

  getActivityGoodsList(applet_uuid: string,
                       page: number,
                       pageSize: number) {
    const url = `${adSystemApiUrl}/v1/member/activity_goods/lists_all` +
      `?applet_uuid=${applet_uuid}&page=${page}&pageSize=${pageSize}`;
    return this.httpService.httpGetAuth(url).map(rs => rs);
  }

  postAddUnit(applet_uuid, showcase_id, name, type, sort, relationID, url) {
    const url1 = `${adSystemApiUrl}/v1/member/show_case_detail/add_v3`;
    const headers = new Headers({'Content-Type': 'application/json'});
    const options = new RequestOptions({headers: headers});
    const body = JSON.stringify({
      applet_uuid: applet_uuid,
      showcase_id: showcase_id,
      name: name,
      type: type,
      sort: sort,
      relationID: relationID,
      url: url,
    });
    return this.httpService.httpPostAuth(url1, body, options).map(rs => rs);
  }

  postEditUnit(id, applet_uuid, showcase_id, name, type, sort, relationID, url) {
    const url1 = `${adSystemApiUrl}/v1/member/show_case_detail/edit_v3`;
    const headers = new Headers({'Content-Type': 'application/json'});
    const options = new RequestOptions({headers: headers});
    const body = JSON.stringify({
      id: id,
      applet_uuid: applet_uuid,
      showcase_id: showcase_id,
      name: name,
      type: type,
      sort: sort,
      relationID: relationID,
      url: url,
    });
    return this.httpService.httpPostAuth(url1, body, options).map(rs => rs);
  }

}
